import { EntityType, UnitType, BuildingType, ClientTildeType, PlayerType } from '../../../battle/Enums';
import { IndexEntityStatsItemData } from '../../../battle/Model';

/**
 * 血条信息接口
 */
export interface HealthBar {
    /** 当前血量 */
    current: number;
    /** 最大血量 */
    max: number;
    /** 是否显示血条 */
    visible: boolean;
}

/**
 * TiledObject初始化配置接口
 */
export interface TiledObjInitConfig {
    id: number;
    playerType: PlayerType;
    playerID?: string;
    entityType: EntityType;
    uType: any;
    position: { x: number; y: number; };
    name?: string;
    healthBar?: Partial<HealthBar>;
    clientTildeType?: ClientTildeType;
}

/**
 * TiledObject数据管理类
 * 负责管理所有与数据相关的属性和逻辑
 */
export class TiledObjectData {
    /** 唯一标识符 */
    public id: number;
    /** 归属哪个玩家 */
    public playerType: PlayerType;
    /** 玩家ID */
    public playerID: string;
    /** 实体类型 */
    public entityType: EntityType;
    /** 实体具体类型 */
    public uType: any;
    /** 位置信息 */
    public position: {x: number; y: number;};
    /** 血条信息 */
    public healthBar: HealthBar;
    /** 客户端显示类型 */
    public clientTildeType?: ClientTildeType;
    /** 是否渲染 */
    public isRender: boolean;
    /** 对象名称 */
    public name: string;

    /**
     * 构造函数
     * @param config 初始化配置
     */
    constructor(config?: TiledObjInitConfig) {
        if (config) {
            this.init(config);
        }
    }

    /**
     * 初始化数据
     * @param config 初始化配置
     */
    public init(config: TiledObjInitConfig): void {
        this.name = config.name || `${config.id}`;
        this.id = config.id;
        this.playerID = config.playerID;
        this.playerType = config.playerType;
        this.entityType = config.entityType;
        this.uType = config.uType;
        this.position = { ...config.position };
        this.clientTildeType = config.clientTildeType;
        this.healthBar = {
            current: config.healthBar?.current ?? 100,
            max: config.healthBar?.max ?? 100,
            visible: config.healthBar?.visible ?? true
        };
        this.isRender = false;
    }

    /**
     * 更新数据
     * @param config 更新配置
     */
    public updateInfo(config: TiledObjInitConfig): void {
        this.id = config.id;
        this.position = { ...config.position };
        this.name = config.name || `${config.id}`;
        this.clientTildeType = config.clientTildeType;
        this.healthBar = {
            current: config.healthBar?.current ?? 100,
            max: config.healthBar?.max ?? 100,
            visible: config.healthBar?.visible ?? true
        };
    }

    /**
     * 更新位置
     * @param newPosition 新位置
     */
    public updatePosition(newPosition: {x: number; y: number;}): void {
        this.position = { ...newPosition };
    }

    /**
     * 更新血量
     * @param newHealth 新血量值
     * @returns 血量变化信息 {oldHealth, newHealth, damageValue?, healValue?}
     */
    public updateHealth(newHealth: number): {oldHealth: number, newHealth: number, damageValue?: number, healValue?: number} {
        const oldHealth = this.healthBar.current;
        this.healthBar.current = Math.max(0, Math.min(newHealth, this.healthBar.max));
        
        const result: any = {
            oldHealth,
            newHealth: this.healthBar.current
        };

        if (newHealth < oldHealth) {
            result.damageValue = oldHealth - newHealth;
        } else if (newHealth > oldHealth) {
            result.healValue = newHealth - oldHealth;
        }

        return result;
    }

    /**
     * 设置最大血量
     * @param maxHealth 最大血量值
     */
    public setMaxHealth(maxHealth: number): void {
        this.healthBar.max = Math.max(1, maxHealth);
        this.healthBar.current = Math.min(this.healthBar.current, this.healthBar.max);
    }

    /**
     * 显示/隐藏血条
     * @param visible 是否显示
     */
    public setHealthBarVisible(visible: boolean): void {
        this.healthBar.visible = visible;
    }

    /**
     * 获取血量百分比
     * @returns 血量百分比 (0-1)
     */
    public getHealthPercentage(): number {
        return this.healthBar.max > 0 ? this.healthBar.current / this.healthBar.max : 0;
    }

    /**
     * 判断是否死亡
     * @returns 是否死亡
     */
    public isDead(): boolean {
        return this.healthBar.current <= 0;
    }

    /**
     * 判断是否为士兵
     * @returns 是否为士兵
     */
    public isSoldier(): boolean {
        return this.clientTildeType === ClientTildeType.soldier;
    }

    /**
     * 判断是否为采集单位
     * @returns 是否为采集单位
     */
    public isCollector(): boolean {
        return this.clientTildeType === ClientTildeType.collect;
    }

    /**
     * 判断是否为治疗兵
     * @returns 是否为治疗兵
     */
    public isDoctor(): boolean {
        return this.clientTildeType === ClientTildeType.doctor;
    }

    /**
     * 判断是否为弓箭手
     * @returns 是否为弓箭手
     */
    public isArcher(): boolean {
        return this.clientTildeType === ClientTildeType.archer;
    }

    /**
     * 判断是否为建造兵
     * @returns 是否为建造兵
     */
    public isWorker(): boolean {
        return this.clientTildeType === ClientTildeType.worker;
    }

    /**
     * 判断是否为基地
     * @returns 是否为基地
     */
    public isBase(): boolean {
        return this.clientTildeType === ClientTildeType.base;
    }

    /**
     * 判断是否为防御塔
     * @returns 是否为防御塔
     */
    public isTower(): boolean {
        return this.clientTildeType === ClientTildeType.tower;
    }

    /**
     * 转换为JSON对象
     * @returns JSON对象
     */
    public toJSON(): object {
        return {
            id: this.id,
            position: this.position,
            healthBar: this.healthBar,
            clientTildeType: this.clientTildeType,
            name: this.name,
            playerType: this.playerType,
            playerID: this.playerID,
            entityType: this.entityType,
            uType: this.uType,
            isRender: this.isRender
        };
    }

    /**
     * 从JSON对象创建实例
     * @param json JSON对象
     * @returns TiledObjInitConfig配置对象
     */
    public static fromJSON(json: any): TiledObjInitConfig {
        const obj: TiledObjInitConfig = {
            id: json.id,
            playerType: json.playerType,
            entityType: json.entityType,
            uType: json.uType,
            position: json.position,
            name: json.name,
            healthBar: json.healthBar,
            clientTildeType: json.clientTildeType,
        };
        return obj;
    }

    /**
     * 创建IndexEntityStatsItemData
     * @returns IndexEntityStatsItemData对象
     */
    public createEntityStatsItemData(): IndexEntityStatsItemData {
        return {
            entityId: this.id,
            playerID: this.playerID,
            entityType: this.entityType,
            playerType: this.playerType
        };
    }

    /**
     * 克隆数据对象
     * @returns 新的TiledObjectData实例
     */
    public clone(): TiledObjectData {
        const config: TiledObjInitConfig = {
            id: this.id,
            playerType: this.playerType,
            playerID: this.playerID,
            entityType: this.entityType,
            uType: this.uType,
            position: { ...this.position },
            name: this.name,
            healthBar: { ...this.healthBar },
            clientTildeType: this.clientTildeType
        };
        return new TiledObjectData(config);
    }

    /**
     * 重置数据到初始状态
     */
    public reset(): void {
        this.isRender = false;
        this.healthBar.current = this.healthBar.max;
    }

    /**
     * 清理数据
     */
    public clear(): void {
        this.id = 0;
        this.playerID = '';
        this.name = '';
        this.position = { x: 0, y: 0 };
        this.healthBar = { current: 0, max: 0, visible: false };
        this.isRender = false;
    }
}